#--------------------------------------------------------------------------------------------------------------------#
#----------------------------------------------- Portmann & Stojanovic ----------------------------------------------#
#--------------------------------------------------- November 2020 --------------------------------------------------#
#------- Are Immigrant-Origin Candidates Penalized in Virtue of Ingroup Favoritism or Outrgoup Hostility? -----------#
#--------------------------------------------------------------------------------------------------------------------#
#------------------------------ Appendix A: Population with a Migration Background ---------------------------------#
#--------------------------------------------------------------------------------------------------------------------#

rm(list=ls())
setwd(".../...")
load("d_nat")

# ------------------------------------------------------------------ Table 1 Appendix: Origin of naturalized Swiss citizens, 1991-2015

names(d_nat)
d_t1 <- d_nat[ ,c("ogn_Italy", "ogn_France", "ogn_Germany", "ogn_Portugal", "ogn_Spain", "ogn_OtherSouthernEurope",
"ogn_EasternEuropean", "ogn_OtherWesternEuropean", "ogn_Yugoslav", "ogn_Albanian", "ogn_OtherHispanic",
"ogn_Indian", "ogn_EasternAsian", "ogn_CentralAsian", "ogn_Turkish", "ogn_OtherArabic_Maghreb",
"ogn_OtherArabic_MiddleEast", "ogn_OtherAfrican", "ogn_StatelessUnbekannt_others", "code", "USCANNZAUS")]

d_t1 <- d_t1 %>% dplyr::summarise_all(funs(sum))
d_t1 <- as.data.frame(t(d_t1[,-20 ]))

d_t1$origin = rownames(d_t1)
d_t1 <- plyr::rename(d_t1, c("V1" = "n"))

#Calculate total and percentage
d_t1 <- d_t1 %>% dplyr::mutate(freq = (n/sum(n))*100)

#Rowsums
x = c(sum(d_t1$n), "Total", sum(d_t1$freq))
d_t1 <- rbind(d_t1,x)

#Order rows and columns
d_t1 <- d_t1[,c(2, 1, 3)]
table(d_t1$origin)
d_t1$origin <- mapvalues(d_t1$origin, from = c("ogn_Yugoslav", "ogn_Italy", "ogn_Turkish", "ogn_Germany", 
"ogn_Portugal", "ogn_France", "ogn_Spain", "ogn_Albanian", "ogn_OtherWesternEuropean", 
"ogn_EasternEuropean", "ogn_OtherSouthernEurope", "ogn_Indian","ogn_EasternAsian", "ogn_OtherArabic_Maghreb", 
"ogn_OtherAfrican", "ogn_OtherHispanic","USCANNZAUS", "ogn_CentralAsian", "ogn_OtherArabic_MiddleEast", 
"ogn_StatelessUnbekannt_others","Total"), 
to = c("Former Yugoslavia", "Italy", "Turkey (including ethnic curds)", 
"Germany", "Portugal", "France", "Spain", "Albania", "Other Western European countries", 
"Eastern European countries", "Other Southern European  countries", "Sri Lanka and India",
"Other Eastern Asian countries", "Maghreb", "Other African countries", "Central and Southern American countries",
"USA, Canada, Australia and New Zealand", "Central Asian countries", "Middle Eastern countries", "Stateless/others", "All"))

d_t1 <- d_t1[c(9, 1, 15, 3, 4, 2, 5, 10, 8, 7, 6, 12, 13, 16, 18, 11, 20, 14, 17, 19, 21), ]

#Finalize
d_t1$freq <- as.numeric(d_t1$freq)
d_t1$freq <- round(d_t1$freq, digits = 1)

kable(as.matrix(d_t1), caption = "Countries and continents of origin of naturalilzed Swiss citizens, 1991-2015", 
col.names = c("", "n", "%"),
row.names = FALSE,
format="latex", booktabs = T)

 
